/* 
 * Arcms基于J2EE架构内容管理系统
 * Copyright © Zrar Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://www.zrar.com/arcms
 */
package org.grayrabbit.cms.dao.user.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.dao.user.UserDao;
import org.grayrabbit.cms.entity.User;
import org.grayrabbit.cms.vo.user.UserBean;
import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;
import org.springframework.stereotype.Repository;

/**
 * <b>User</b> dao implementation 
 * @version 1.0，创建时间：2011-05-16 16:04:46 
 */
@Repository
public class UserDaoImpl extends GenericHibernateDao<User, String>
		implements UserDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From User as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see UserDao#assembly(UserBean)
	 */
	public QueryCondition assembly(UserBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see UserDao#assemblyCount(UserBean)
	 */
	public QueryCondition assemblyCount(UserBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see UserDao#count(UserBean)
	 */
	public int count(UserBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see UserDao#assemblyNameQuery(UserBean)
	 */
	public QueryCondition assermblyNameQuery(UserBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see UserDao#assemblyNameQueryCount(UserBean)
	 */
	public QueryCondition assemblyNameQueryCount(UserBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see UserDao#findByPage(Page, UserBean)
	 */
	public List<User> findByPage(Page<User> page,
			UserBean bean) {
		QueryCondition qc = assembly(bean);
		qc.append(" and obj.createTime is not null");
		qc.appenOrderBy(" obj.createTime desc");
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		List<User> list = findByQc(qc);
		return list;
	}
	// 业务查询主要逻辑代码
	private void transNameQuery(UserBean bean,QueryCondition qc){
		if(bean!=null){
			 if(StringUtils.isNotBlank(bean.getUserLoginName())){
					qc.add("And obj.userLoginName = ? ", bean.getUserLoginName().trim());
				}
	            if(StringUtils.isNotBlank(bean.getUserRealName())){
					qc.add("And obj.userRealName like ? ", "%"+bean.getUserRealName().trim()+"%");
				}
	            if(StringUtils.isNotBlank(bean.getUserPapersType())){
					qc.add("And obj.userPapersType = ? ", bean.getUserPapersType());
				}
	            if(StringUtils.isNotBlank(bean.getUserPapersNum())){
					qc.add("And obj.userPapersNum like ? ", "%"+bean.getUserPapersNum()+"%");
				}
	            if(StringUtils.isNotBlank(bean.getUserPhone())){
					qc.add("And obj.userPhone like ? ",bean.getUserPhone().trim());
				}
	            if(StringUtils.isNotBlank(bean.getUserAddress())){
					qc.add("And obj.userAddress like ? ", "%"+bean.getUserAddress()+"%");
				}
	            if(StringUtils.isNotBlank(bean.getUserIntroduction())){
					qc.add("And obj.userIntroduction like ? ", "%"+bean.getUserIntroduction()+"%");
				}
	            if(bean.getCreateTime() != null){
					qc.add("And obj.createTime = ? ", bean.getCreateTime());
				}
	            if(StringUtils.isNotBlank(bean.getEmail())){
					qc.add("And obj.email like ? ", "%"+bean.getEmail()+"%");
				}
	            if(bean.getMale() != null){
					qc.add("And obj.male = ? ", bean.getMale());
				}
	            if(StringUtils.isNotBlank(bean.getMobile())){
					qc.add("And obj.mobile like ? ", "%"+bean.getMobile()+"%");
				}
		}
	}
	
	/**
	 * true:已经存在
	 * false：可用
	 */
	public boolean checkNameUnique(String userLoginName,String userId){
		boolean unique=false;
		List<User> userList=null;
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(StringUtils.isNotBlank(userLoginName)){
			qc.add(" And obj.userLoginName=?", userLoginName);
			userList=findByQc(qc);
		}
		if(StringUtils.isNotBlank(userId)){
			if(userList!=null&&(!userList.isEmpty())){
				if(userList.size()>=2||!userList.get(0).getUserId().equals(userId)){
					unique=true;//不可用
				}
			}
		}else{
			if((userList!=null)&&(!userList.isEmpty())){
				unique=true;  //不可用
			}
		}
		return unique;
	}
	
	/**
	 * 根据用户名获取用户
	 * @param userLoginName
	 * @return
	 */
	public List<User> getUserByLoginName(String userLoginName){
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(StringUtils.isNotBlank(userLoginName)){
			qc.add(" And obj.userLoginName = ? ", userLoginName);
		}
		return findByQc(qc);
	}
	/**
	 * 根据用户邮箱获取用户
	 * @param email
	 * @return
	 */
	public List<User> getUserByEmail(String email){
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(StringUtils.isNotBlank(email)){
			qc.add(" And obj.email = ? ", email);
		}
		return findByQc(qc);
	}
}